<template>
	<view class="user">
		<view class="top-bg"></view>
		<view class="header">
			<view class="btn msg" @click="onPageTap('msg')">
				<i class="remind" v-if="newRemind"></i>
			</view>
			<view class="btn set" @click="onPageTap('set')"></view>
		</view>
		<!-- 已经登陆 -->
		<view class="person" v-if="pageObj.token" @click="onPageTap('info')">
			<image class="avatar-default" :src="dataObj.avatar || dataObj.avatarDefault"></image>
			<view class="p-data">
				<view class="name ellipsis-1">{{dataObj.nickname || '暂未命名'}}</view>
				<view class="mobile">{{dataObj.mobile || '-'}}</view>
			</view>
			<view class="info">个人信息</view>
		</view>
		<!-- 未登陆 -->
		<view class="person" v-if="!pageObj.token">
			<image class="avatar-unlogin" src="../../static/images/user/avatar.png" @click="onPageTap('login')"></image>
		</view>
		<!-- 我的资产 -->
		<view class="block">
			<view class="title">我的资产</view>
			<view class="assets">
				<view class="assets-item" @click="onPageTap('balance')">
					<view class="data">{{dataObj.balance || '0'}}</view>
					<view class="text">我的余额</view>
				</view>
				<view class="assets-item" @click="onPageTap('coupon')">
					<view class="data">{{dataObj.coupon_count || '0'}}</view>
					<view class="text">优惠券</view>
				</view>
				<view class="assets-item" @click="onPageTap('order')">
					<view class="data">{{dataObj.order_count || '0'}}</view>
					<view class="text">我的订单</view>
				</view>
			</view>
		</view>
		<view class="block">
			<view class="title">我的功能</view>
			<view class="f-list">
				<view class="f-item" v-for="item in pageObj.fnList" :key="item.key" @click="onPageTap(item.key)">
					<image class="icon" :src="'../../static/images/user/' + item.key + '.png'"></image>
					<view class="name">{{item.name}}</view>
				</view>
			</view>
		</view>
		<view class="block" v-if="dataObj.nickname === '伟豪'">
			<view class="title">我的小功能</view>
			<view class="f-list">
				<view class="f-item" @click="onPageTap('video')">
					<image class="icon" src="../../static/images/user/video.png"></image>
					<view class="name">我的视频</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import avatarDefault from '@/static/images/user/avatar-default.png';
	import {
		getInfoData
	} from '@/common/tools.js';

	export default {
		data() {
			return {
				pageObj: {
					token: uni.getStorageSync('token'),
					fnList: [{
							key: 'course',
							name: '我的课程'
						},
						{
							key: 'question',
							name: '我的题库'
						}, {
							key: 'wrong',
							name: '我的错题'
						}, {
							key: 'exam',
							name: '我的考试'
						},
						{
							key: 'report',
							name: '我的报名'
						}, {
							key: 'enclosure',
							name: '我的附件'
						}, {
							key: 'company',
							name: '我的足迹'
						}, {
							key: 'certificate',
							name: '我的证书'
						},
						{
							key: 'userlist',
							name: '用户列表'
						},
						{
							key: 'follow',
							name: '关注我的企业'
						},
						{
							key: 'service',
							name: '客服',
						}
					],
					urlObj: {
						login: '/pages/account/login', // 登陆
						msg: './msg', // 消息
						set: './set/index', // 设置
						info: './info/info', // 个人信息
						balance: './assets/balance', // 我的余额
						coupon: './assets/coupon', // 优惠券
						order: './assets/order-list', // 我的订单
						certificate: './my/certificate', // 我的证书
						course: './my/course', // 我的课程
						video: './my/video', // 我的视频
						question: './my/question', // 我的题库
						wrong: './my/wrong', // 我的错题
						exam: './my/exam', // 我的考试
						report: './my/report', // 我的报名
						enclosure: './my/enclosure', // 我的附件
						company: './my/footprint', // 我的企业
						userlist: './my/userlist', //用户列表
						follow: './my/follow', //关注我的企业
						service: './my/service' //我的客服
					}
				},
				dataObj: {
					nickname: '',
					mobile: '',
					avatar: '',
					avatarDefault: avatarDefault,
					balance: '0.00',
					coupon: 0,
					order_count: '',
				},
				newRemind: false
			}
		},
		onLoad() {
			// #ifdef MP-WEIXIN
			wx.showShareMenu({
				withShareTicket: true,
				//设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
				menus: ["shareAppMessage", "shareTimeline"]
			});
			// #endif
			this.getRemind();
		},
		onShow() {
			this.getData(); // 获取数据
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.getData();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {
			// 页面跳转
			onPageTap(key) {
				const url = this.pageObj['urlObj'][key];
				if (!url) return false;
				uni.navigateTo({
					url: url
				});
			},
			// 获取数据
			getData() {
				// 从缓存中获取
				getInfoData((res) => {
					let that = this;
					that.dataObj = res;
					if (that.dataObj.avatar === './../public/static/images/default.png') {
						that.dataObj.avatar = avatarDefault;
					}
					if (that.dataObj.isQiye === 2) {
						that.pageObj.fnList.filter((item) => {
							if (item.key === 'userlist') {
								var arr = that.pageObj.fnList.splice(8, 1);
							}
						})
					}
				});
			},
			// 获取消息
			getRemind() {
				const that = this;
				this.$Requset({
					url: this.$Api['user']['msg'],
					data: {},
					loading: false,
					success: (res) => {
						const {
							data
						} = res;
						this.newRemind = data.length > 0;
						setTimeout(() => {
							that.getRemind();
						}, 10000);
					},
					fail: (err) => {
						console.log(err);
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f5f5f5;
	}

	.user {
		padding: 0 36rpx;

		.top-bg {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 380rpx;
			background: linear-gradient(180deg, #FEF2E9 0%, #F7F7F7 100%);
		}

		.header {
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			font-size: 0;
			text-align: right;

			.btn {
				display: inline-block;
				position: relative;
				width: 40rpx;
				height: 40rpx;
				margin-left: 30rpx;
				vertical-align: middle;
				background-repeat: no-repeat;
				background-position: center;
				background-size: 100%;
			}

			.msg {
				background-image: url(../../static/images/user/msg.png);

				.remind {
					position: absolute;
					top: -4rpx;
					right: -4rpx;
					height: 20rpx;
					width: 20rpx;
					border-radius: 50%;
					background-color: #F21B3F;
				}
			}

			.set {
				background-image: url(../../static/images/user/set.png);
			}
		}

		.person {
			position: relative;
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: right;
			padding-top: 18rpx;
			margin-bottom: 42rpx;

			.avatar-unlogin {
				position: relative;
				float: left;
				width: 100rpx;
				height: 100rpx;
				padding-right: 152rpx;

				&::after {
					content: '点击登录';
					position: absolute;
					top: 0;
					right: 0;
					line-height: 100rpx;
					width: 152rpx;
					font-size: 32rpx;
					font-weight: bold;
					color: #1A1A1A;
					text-align: right;
					vertical-align: middle;
				}
			}

			.avatar-default {
				float: left;
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
				margin-right: 20rpx;
				background-color: #e5e5e5;
			}

			.p-data {
				float: left;
				width: 200rpx;
				text-align: left;
				padding-top: 8rpx;

				.name {
					height: 46rpx;
					line-height: 46rpx;
					font-size: 32rpx;
					font-weight: bold;
					color: #1A1A1A;
					margin-bottom: 4rpx;
				}

				.mobile {
					line-height: 36rpx;
					font-size: 24rpx;
					color: #7F7F7F;
				}
			}

			.info {
				position: relative;
				display: inline-block;
				width: 200rpx;
				height: 36rpx;
				line-height: 36rpx;
				font-size: 26rpx;
				padding-right: 28rpx;

				&::after {
					content: '';
					position: absolute;
					top: 0;
					right: 0;
					width: 26rpx;
					height: 36rpx;
					background-repeat: no-repeat;
					background-position: center;
					background-size: 26rpx 26rpx;
					background-image: url(../../static/images/common/arrow-right.png);
				}
			}
		}

		.block {
			position: relative;
			padding: 32rpx 0;

			.title {
				line-height: 40rpx;
				font-size: 30rpx;
				color: #1A1A1A;
				margin-bottom: 28rpx;
				padding-left: 30rpx;
			}

			.assets {
				display: flex;

				&-item {
					position: relative;
					flex: 1;
					text-align: center;

					&::after {
						content: '';
						position: absolute;
						top: 50%;
						margin-top: -28rpx;
						right: 0;
						height: 56rpx;
						width: 1rpx;
						background-color: #EAEFF1;
					}

					&:nth-last-child(1)::after {
						top: 0;
						margin-right: 0;
						height: 0;
						width: 0;
					}

					.data {
						line-height: 46rpx;
						font-size: 32rpx;
						font-weight: bold;
						color: #1A1A1A;
					}

					.text {
						line-height: 32rpx;
						font-size: 22rpx;
						color: #373737;
					}
				}
			}

			.f-list {
				font-size: 0;
				padding-top: 12rpx;

				.f-item {
					display: inline-block;
					width: 25%;
					text-align: center;
					margin-bottom: 48rpx;

					.icon {
						width: 54rpx;
						height: 54rpx;
						margin-bottom: 10rpx;
					}

					.name {
						line-height: 30rpx;
						font-size: 22rpx;
						color: #373737;
					}
				}
			}
		}
	}
</style>
